#include "data_demon.h"

head *initfly()
{
    head *p = (head *)malloc(sizeof(head));
    if (p == NULL)
    {
        printf("申请空间失败\n");
        return NULL;
    }
    fnode *head_node = (fnode *)malloc(sizeof(fnode));
    if (head_node == NULL)
    {
        printf("申请表头空间失败\n");
        free(p);
        return NULL;
    }
    head_node->before = head_node;
    head_node->after = head_node;
    p->head = head_node;
    return p;
}

void Inputflydata() // 增加航班信息到文件中
{
    flight_t data = {0};
    printf("请输入航班班次:");
    scanf("%d", &data.id);
    printf("航班起点站:");
    scanf("%s", data.startcity);
    printf("起飞时间:");
    scanf("%s", data.starttime);
    printf("航班终点站:");
    scanf("%s", data.arrivecity);
    printf("到达时间:");
    scanf("%s", data.arrivetime);
    printf("飞行时间:");
    scanf("%s", data.flytime);
    printf("票价:");
    scanf("%f", &data.value);
    printf("额定载客量:");
    scanf("%d", &data.maxNUM);
    printf("剩余座位数量:");
    scanf("%d", &data.leftbuyersa);
    printf("是否起飞('是' or '否'):");
    scanf("%4s", data.whetherfly);
    FILE *fp = fopen("航班信息.txt", "a+");
    fprintf(fp, "%d\t%s\t%s\t%s\t%s\t%s\t%.2f\t%d\t%d\t%s\n", data.id, data.startcity, data.starttime, data.arrivecity, data.arrivetime, data.flytime, data.value, data.maxNUM, data.leftbuyersa, data.whetherfly);
    fclose(fp);
    return;
}

head *readfly() // 从文件中读取航班信息到链表中
{
    head *p = initfly();
    fnode *L = p->head;
    p->head = L;
    FILE *fp = fopen("航班信息.txt", "r");
    do
    {
        L->after = (fnode *)malloc(sizeof(fnode));
        L->after->before = L;
        L->after->after = NULL;
        L = L->after;
        fscanf(fp, "%d\t%s\t%s\t%s\t%s\t%s\t%f\t%d\t%d\t%s\n", &L->data.id, L->data.startcity, L->data.starttime, L->data.arrivecity, L->data.arrivetime, L->data.flytime, &L->data.value, &L->data.maxNUM, &L->data.leftbuyersa, L->data.whetherfly);
        if (feof(fp))
        {
            break;
        }

    } while (!feof(fp));
    L->after = p->head;
    p->head->before = L;
    fclose(fp);
    return p;
}

void deletoutflydata(head *p) // 从文件中删除航班信息
{
    p = readfly();
    fnode *Y = p->head;
    ptf(p);
    printf("请输入要删除的航班班次\n");
    int id = 0;
    scanf("%d", &id);
    do
    {
        if (Y->data.id == id)
        {
            fnode *K = Y->before;
            fnode *J = Y->after;
            K->after = J;
            J->before = K;
            free(Y);
        }
        Y = Y->after;
    } while (Y != p->head);
    fnode *L = p->head->after;
    FILE *fp = fopen("航班信息.txt", "w");
    do
    {
        fprintf(fp, "%d\t%s\t%s\t%s\t%s\t%s\t%f\t%d\t%d\t%s\n", L->data.id, L->data.startcity, L->data.starttime, L->data.arrivecity, L->data.arrivetime, L->data.flytime, L->data.value, L->data.maxNUM, L->data.leftbuyersa, L->data.whetherfly);
        L = L->after;
    } while (L != p->head);
    fclose(fp);
    fp = NULL;
}

void ptf(head *p) // 打印航班信息的函数
{
    if (p == NULL || p->head == NULL || p->head == p->head->after)
    {
        printf("链表为空\n");
        return;
    }
    fnode *L = p->head->after;
    printf("航班班次\t航班起点站\t起飞时间\t航班终点站\t到达时间\t飞行时间\t票价\t\t额定载客量\t剩余座位数量\t是否起飞\n");
    while (L != p->head)
    {
        printf("%d\t\t", L->data.id);
        printf("%s\t\t", L->data.startcity);
        printf("%s\t\t", L->data.starttime);
        printf("%s\t\t", L->data.arrivecity);
        printf("%s\t\t", L->data.arrivetime);
        printf("%s\t\t", L->data.flytime);
        printf("%.2f\t\t", L->data.value);
        printf("%d\t\t", L->data.maxNUM);
        printf("%d\t\t", L->data.leftbuyersa);
        printf("%s\n", L->data.whetherfly);
        L = L->after;
    }
    return;
}

void changeflytickt(head *p, unode *user, int num) // 乘客购票或退票
{
    if (num == 1)
    {
        ptf(p);
        printf("请输入要购买的航班班次\n");
        int id = 0;
        scanf("%d", &id);
        fnode *L = p->head->after;
        while (L != NULL)
        {
            if (L->data.id == id)
            {
                L->data.leftbuyersa--;
                flight_t data = L->data;
                L = p->head->after;
                FILE *lp = fopen("航班信息.txt", "w");
                do
                {
                    fprintf(lp, "%d\t%s\t%s\t%s\t%s\t%s\t%f\t%d\t%d\t%s\n", L->data.id, L->data.startcity, L->data.starttime, L->data.arrivecity, L->data.arrivetime, L->data.flytime, L->data.value, L->data.maxNUM, L->data.leftbuyersa, L->data.whetherfly);
                    L = L->after;
                } while (L != p->head);
                fclose(lp);
                lp = NULL;

                printf("购票成功\n");
                data.leftbuyersa = 1;
                char S[20] = {0};
                sprintf(S, "%s.txt", user->userm.name);
                FILE *fp = fopen(S, "a");
                fprintf(fp, "%d\t%s\t%s\t%s\t%s\t%s\t%.2f\t%d\t%d\t%s\n", data.id, data.startcity, data.starttime, data.arrivecity, data.arrivetime, data.flytime, data.value, data.maxNUM, data.leftbuyersa, data.whetherfly);
                fclose(fp);
                break;
            }
            L = L->after;
        }
        return;
    }
    else
    {
        char S[20] = {0};
        int num = 1;
        sprintf(S, "%s.txt", user->userm.name);
        if (fopen(S, "r") == NULL)
        {
            printf("无购票记录\n");
            return;
        }
        head *filedata = initfly();
        fnode *K = filedata->head;
        FILE *fp = fopen(S, "r");
        while (!feof(fp))
        {
            K->after = (fnode *)malloc(sizeof(fnode));
            K->after->before = K;
            K->after->after = NULL;
            K = K->after;
            fscanf(fp, "%d\t%s\t%s\t%s\t%s\t%s\t%f\t%d\t%d\t%s\n", &K->data.id, K->data.startcity, K->data.starttime, K->data.arrivecity, K->data.arrivetime, K->data.flytime, &K->data.value, &K->data.maxNUM, &K->data.leftbuyersa, K->data.whetherfly);
        }
        K->after = filedata->head;
        filedata->head->before = K;
        fclose(fp);
        K = filedata->head;
        ptf(filedata);
        printf("请输入要退票的航班班次\n");
        int id = 0;
        scanf("%d", &id);
        fp = fopen(S, "w");
        do
        {
            K = K->after;
            if (K->data.id == id && num == 1)
            {
                num = 0;
                fnode *Kbefore = K->before;
                fnode *Kafter = K->after;
                Kbefore->after = Kafter;
                Kafter->before = Kbefore;
                free(K);
            }
        } while (K != filedata->head);
        K = filedata->head->after;
        while (K != filedata->head)
        {
            fprintf(fp, "%d\t%s\t%s\t%s\t%s\t%s\t%f\t%d\t%d\t%s\n", K->data.id, K->data.startcity, K->data.starttime, K->data.arrivecity, K->data.arrivetime, K->data.flytime, K->data.value, K->data.maxNUM, K->data.leftbuyersa, K->data.whetherfly);
            K = K->after;        
        }
        fclose(fp);
        fnode *L = p->head->after;
        FILE *lp = fopen("航班信息.txt", "w");
        while (L != p->head)
        {
            if (L->data.id == id)
            {
                L->data.leftbuyersa++;
            }
            fprintf(lp, "%d\t%s\t%s\t%s\t%s\t%s\t%f\t%d\t%d\t%s\n", L->data.id, L->data.startcity, L->data.starttime, L->data.arrivecity, L->data.arrivetime, L->data.flytime, L->data.value, L->data.maxNUM, L->data.leftbuyersa, L->data.whetherfly);
            L = L->after;
        }
        fclose(lp);
    }
}

head * readtickuser(unode *user)
{
     char S[20] = {0};
        int num = 1;
        sprintf(S, "%s.txt", user->userm.name);
         head *filedata = initfly();
        fnode *K = filedata->head;
        FILE *fp = fopen(S, "r");
        while (!feof(fp))
        {
            K->after = (fnode *)malloc(sizeof(fnode));
            K->after->before = K;
            K->after->after = NULL;
            K = K->after;
            fscanf(fp, "%d\t%s\t%s\t%s\t%s\t%s\t%f\t%d\t%d\t%s\n", &K->data.id, K->data.startcity, K->data.starttime, K->data.arrivecity, K->data.arrivetime, K->data.flytime, &K->data.value, &K->data.maxNUM, &K->data.leftbuyersa, K->data.whetherfly);
        }
        K->after = filedata->head;
        filedata->head->before = K;
        fclose(fp);
        return filedata;
}

void sort(head *p)      //排序函数
{
    fnode *K = NULL;
    fnode *L = NULL;
    for (K = p->head->after; K->after != p->head; K = K->after)
    {
        for (L = K->after; L != p->head; L = L->after)
        {
            if (K->data.id > L->data.id)
            {
                flight_t temp;
                temp = K->data;
                K->data = L->data;
                L->data = temp;
           }
       }
   }
   ptf(p);
}

